/**
 * 
 */
package com.zzu.icukangfu.serviceimpl;

import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.zzu.icukangfu.entity.Patient;
import com.zzu.icukangfu.entity.PatientTM;
import com.zzu.icukangfu.service.PatientService;

/**
 * @author teng
 * @time下午11:07:50
 * @corporation ttkj
 */
public class PatientServiceImpl implements PatientService {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.zzu.icukangfu.service.PatientService#addPatient(com.zzu.icukangfu.entity.
	 * Patient)
	 */
	@Override
	@Before(Tx.class)
	public Boolean addPatient(Patient patient) {

		String sql = "insert into patient(patient_name,patient_age,patient_casenum,patient_disease,patient_training_date,username) values(?,?,?,?,now(),?)";

		try {
			return Db.update(sql, patient.getStr("patient_name"), patient.getInt("patient_age"),
					patient.getStr("patient_casenum"), patient.getStr("patient_disease"),
					patient.getStr("username")) == 1 ? true : false;
		} catch (Exception e) {
			// TODO: handle exception

			e.printStackTrace();
		}
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.zzu.icukangfu.service.PatientService#findThePatient(java.lang.String)
	 */
	@Override
	public Patient findThePatient(String casenum) {

		String sql = "select * from patient where patient_casenum=?";
		return Patient.dao.findFirst(sql, casenum);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.zzu.icukangfu.service.PatientService#findThePatient(java.lang.String)
	 */
	@Override
	public Patient findThePatient(Long patient_id) {

		String sql = "select * from patient where patient_id=?";
		return Patient.dao.findFirst(sql, patient_id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.zzu.icukangfu.service.PatientService#addPatientTrainingMessage(com.zzu.
	 * icukangfu.entity.PatientTM)
	 */
	@Override
	@Before(Tx.class)
	public Boolean addPatientTrainingMessage(PatientTM patientTM) {
		// TODO Auto-generated method stub
		String sql = "insert into patient_tm(" + "patient_id," + "patient_heartrate," + "patient_respiratoryrate,"
				+ "patient_myoelectricity," + "patient_presure," + "patient_trainingtime," + "machine_id) "
				+ "values(?,?,?,?,?,now(),?)";

		try {
			return Db.update(sql, patientTM.getLong("patient_id"), patientTM.getInt("patient_heartrate"),
					patientTM.getInt("patient_respiratoryrate"), patientTM.getDouble("patient_myoelectricity"),
					patientTM.getDouble("patient_presure"), patientTM.getStr("machine_id")) == 1 ? true : false;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.zzu.icukangfu.service.PatientService#findThePatientTrainingMessage(java.
	 * lang.String)
	 */
	@Override
	public List<Patient> findThePatientTrainingMessage(String casenum) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Patient> findThePatientTrainingMessage(Long patient_id) {
		/*
		 * 查询60天以内的数据
		 */
		String sql = "select a.*,b.* from patient_tm as a left join patient as b on a.patient_id=b.patient_id where a.patient_id=? and date_sub(curdate(), INTERVAL 60 DAY) <= date_format(a.patient_trainingtime,'%Y-%m-%d') order by a.patient_trainingtime desc";
		return Patient.dao.find(sql, patient_id);
	}

	@Override
	@Before(Tx.class)
	public Patient loginPatient(Patient patient) {
		String sql = "select * from patient where patient_username=? and patient_password=?";
		// TODO Auto-generated method stub
		System.out.println(patient.getStr("patient_username") + patient.getStr("patient_password"));
		System.out.println(Patient.dao.findFirst(sql, "1", "1"));
		return Patient.dao.findFirst(sql, patient.getStr("patient_username"), patient.getStr("patient_password"));
	}

	@Override
	public Patient findByUsername(String username) {
		String sql = "select * from patient where username=? ";
		return Patient.dao.findFirst(sql, username);
	}

}
